//
//  THJTabBarController.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/9/12.
//  Copyright © 2018年 河南德艺. All rights reserved.
//

#import "THJTabBarController.h"
#import "THJMainController.h"
#import "THJDataVC.h"
#import "THJNewsVC.h"
#import "THJUserVC.h"
#import "THJDianJiaVC.h"
#import "THJLoginVC.h"

@interface THJTabBarController ()

@end

@implementation THJTabBarController
#pragma mark ----LBTabBarController单例-----
static THJTabBarController *_sharedTabBarC = nil;

+ (instancetype)sharedTabBarC {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedTabBarC = [[super allocWithZone:NULL] init];
    });
    
    return _sharedTabBarC;
}

+ (id)allocWithZone:(struct _NSZone *)zone {
    return [THJTabBarController sharedTabBarC];
}

- (id)copyWithZone:(struct _NSZone *)zone {
    return [THJTabBarController sharedTabBarC];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    UITabBar *tabBar = [UITabBar appearance];
    [tabBar setBarTintColor:COLOR(65, 66, 67, 1)];
    
    tabBar.translucent = NO;
    THJMainController *vc1 = [[THJMainController alloc]init];
    vc1.tabBarItem.title = @"首页";
    vc1.tabBarItem.selectedImage = [[UIImage imageNamed:@"homeBtn2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc1.tabBarItem.image = [[UIImage imageNamed:@"homeBtn"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController * nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
    
    THJDataVC *vc2 = [[THJDataVC alloc]init];
    vc2.tabBarItem.title = @"商城";
    vc2.tabBarItem.selectedImage = [[UIImage imageNamed:@"storeBtn2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    vc2.tabBarItem.image = [[UIImage imageNamed:@"storeBtn"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    UINavigationController * nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];
    
    THJDianJiaVC *vc3 = [[THJDianJiaVC alloc]init];
    vc3.tabBarItem.title = @"点价";
    vc3.tabBarItem.image = [[UIImage imageNamed:@"沙漏(1)"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    vc3.tabBarItem.selectedImage = [[UIImage imageNamed:@"沙漏"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    UINavigationController * nav3 = [[UINavigationController alloc] initWithRootViewController:vc3];
    
    THJNewsVC *vc4 = [[THJNewsVC alloc]init];
    vc4.tabBarItem.title = @"资讯";
    vc4.tabBarItem.image = [[UIImage imageNamed:@"newsBtn"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    vc4.tabBarItem.selectedImage = [[UIImage imageNamed:@"newsBtn2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    UINavigationController * nav4 = [[UINavigationController alloc] initWithRootViewController:vc4];
    
    THJUserVC * vc5 = [[THJUserVC alloc]init];
    vc5.tabBarItem.title = @"我的";
    vc5.tabBarItem.image = [[UIImage imageNamed:@"mineBtn"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    vc5.tabBarItem.selectedImage = [[UIImage imageNamed:@"minBtn2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    UINavigationController * nav5 = [[UINavigationController alloc] initWithRootViewController:vc5];
    
    // 添加子视图到tabbar
    self.viewControllers = @[nav1, nav2,nav3, nav4, nav5];

    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:THJOrgColor, NSForegroundColorAttributeName,nil] forState:UIControlStateSelected];
    
}
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)Item{
    NSLog(@"点击了llll");
//    if([Item.title isEqual:@"我的"]){
//        NSString * token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
//        if(token.length == 0){
//            THJLoginVC * vc = [[THJLoginVC alloc] init];
//            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
//            [self presentViewController:nav animated:YES completion:nil];
//           
//        }else{
//            NSLog(@"已登录");
//          
//        }
//    }else{
//        
//    }
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSLog(@"点击了");
}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
     NSLog(@"点击了我的");
    if([viewController.tabBarItem.title isEqual:@"我的"]){
        NSLog(@"点击了我的");
        NSString * token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
        if(token.length == 0){
            THJLoginVC * vc = [[THJLoginVC alloc] init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
            [self presentViewController:nav animated:YES completion:nil];
            return NO;
        }else{
            NSLog(@"已登录");
            return YES;
        }
    }else{
        return YES;
    }
}
-(void)alertLogin{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self actionSheetIndex1Function];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}
-(void)actionSheetIndex1Function{
    NSLog(@"好的");
    [self presentViewController:[THJLoginVC new] animated:YES completion:nil];
}
-(void)actionSheetIndex2Function{
    NSLog(@"取消");
}
@end
